package com.example.verifysdk.creatcode;

import android.content.Context;

import com.example.verifysdk.BaseJson;
import com.example.verifysdk.Contents;
import com.example.verifysdk.LogUtil;
import com.example.verifysdk.ResponseException;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by LiuV on 2017/5/5.
 */

public class CreatCodePresenter {
    private static final String TAG = "CreatCodePresenter";

    private CreatCodeView mCreatCodeView;
    private CreatCodeModel mCreatCodeModel;
    private Gson gson = new Gson();

    public void setmCreatCodeView(CreatCodeView mCreatCodeView) {
        this.mCreatCodeView = mCreatCodeView;
    }

    public void login(String partner, String partnerRemark, String totp) {
        if (mCreatCodeModel == null) {
            mCreatCodeModel = new CreatCodeModel();
        }
        mCreatCodeModel.login(mCreatCodeView.getViewContext(), partner, partnerRemark, totp,
                new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        mCreatCodeView.onFail(new ResponseException(e));
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String res = response.body().string();
                        if (res.contains(Contents.ERROR_TAG_1) || !res.contains(Contents.ERROR_TAG_2)) {
                            this.onFailure(call, new IOException("服务器响应异常"));
                            return;
                        }
                        BaseJson baseJson = gson.fromJson(res, BaseJson.class);
                        if (baseJson == null) {
                            mCreatCodeView.onFail(new ResponseException("服务器响应异常"));
                            return;
                        }
                        if (baseJson.getCode() == 0) {
                            LoginObjBean bean = gson.fromJson(baseJson.getObj(), LoginObjBean.class);
                            switch (bean.getAppIsVerify()){
                                case 0:
                                    mCreatCodeView.userNotRegistered();
                                    break;
                                case 1:
                                case 2:
                                    mCreatCodeView.loginSuccess(bean);
                                    break;
                            }
                        } else if (baseJson.getCode() == 1003) {
                            mCreatCodeView.userNotRegistered();
//                        }else if (baseJson.getCode() == 105){
//                            mCreatCodeView.onFail(new ResponseException("请8秒后重试...0"));
                        }else {
                            mCreatCodeView.onFail(new ResponseException(baseJson.getError()));
                        }
                    }
                });
    }

    public void getOnlineSecret() {
        if (mCreatCodeModel == null) {
            mCreatCodeModel = new CreatCodeModel();
        }
        mCreatCodeModel.applyOnlineSecret(mCreatCodeView.getViewContext(), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                LogUtil.i(TAG, "---getOnlineSecret---onFailure===" + e.getMessage());
                mCreatCodeView.onFail(new ResponseException(e.getMessage()));
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                LogUtil.i(TAG, "---getOnlineSecret---onResponse===" + response.toString());
                String res = response.body().string();
                if (res.contains(Contents.ERROR_TAG_1) || !res.contains(Contents.ERROR_TAG_2)) {
                    this.onFailure(call, new IOException("服务器响应异常"));
                    return;
                }
                LogUtil.i(TAG, "---getOnlineSecret---onResponse==res==" + res);
                BaseJson baseJson = gson.fromJson(res, BaseJson.class);
                if (baseJson == null || baseJson.getCode() != 0) {
                    if (baseJson.getCode() == 99){
                        mCreatCodeView.reLogin();
                        return;
                    }
                    mCreatCodeView.onFail(
                            new ResponseException(baseJson)
                    );
                } else {
                    String sec = gson.fromJson(baseJson.getObj(), String.class);
                    mCreatCodeView.getSecretSuccess(sec);
                }
            }
        });
    }

    public interface CreatCodeView {
        void getSecretSuccess(String sceret);

        void onFail(ResponseException e);

        void loginSuccess(LoginObjBean bean);

        Context getViewContext();

        void userNotRegistered();

        void reLogin();
    }
}
